package com.cooperate.fly.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.cooperate.fly.bo.Case;
import com.cooperate.fly.service.buildDB.BuildCaseService;
import com.cooperate.fly.util.SystemConstant;
import com.cooperate.fly.web.Result;

@Controller
@RequestMapping("/case")
@SessionAttributes(SystemConstant.USER_SESSION_KEY)
public class CaseDesignController extends BaseController {

	@Resource
	BuildCaseService buildCaseService;

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	@ResponseBody
	public Result add(Case _case) {
		Result res = buildCaseService.createCaseNode(_case);
		return res;
	}

	@RequestMapping(value = "/update", method = RequestMethod.GET)
	@ResponseBody
	public Result update(Case _case) {
		// update
		Result res = buildCaseService.updateCaseNode(_case);
		return res;

	}

	@RequestMapping(value = "/delete")
	@ResponseBody
	public Result delete(@RequestParam(value = "id", required = true) int id) {
		Result res = buildCaseService.deleteCaseNodeById(id);
		return res;
	}

	// 访问某一数据库中的所有实例列表（含其所在分类）
	@RequestMapping(value = "/name/{schemaNames}", produces = "application/json;charset=utf-8", method = RequestMethod.GET, params = "action=get")
	@ResponseBody
	public String visitCases(@PathVariable String schemaNames) {
		return buildCaseService.visitCases(schemaNames);
	}

	// 三维表：根据xy的值获取z的值
	@RequestMapping(value = "/caseid/{caseID}/case/field/{field}/case/xvalue/{xValue}/case/yvalue/{yValue}", produces = "application/json;charset=utf-8", method = RequestMethod.GET, params = "action=get")
	@ResponseBody
	public String findZValue(@PathVariable String caseID,
			@PathVariable String field, @PathVariable String xValue,
			@PathVariable String yValue) {
		return buildCaseService.findZValue(caseID, field, xValue, yValue);
	}

	// 三维表：根据输入x值返回
	@RequestMapping(value = "/caseid/{caseID}/case/field/{field}/case/xvalue/{xValue}", produces = "application/json;charset=utf-8", method = RequestMethod.GET, params = "action=get")
	@ResponseBody
	public String findXValue(@PathVariable String caseID,
			@PathVariable String field, @PathVariable String xValue) {
		return buildCaseService.findXValue(caseID, field, xValue);
	}

	// 三维表：根据输入Y值返回
	@RequestMapping(value = "/caseid/{caseID}/case/field/{field}/case/yvalue/{yValue}", produces = "application/json;charset=utf-8", method = RequestMethod.GET, params = "action=get")
	@ResponseBody
	public String findYValue(@PathVariable String caseID,
			@PathVariable String field, @PathVariable String yValue) {
		return buildCaseService.findYValue(caseID, field, yValue);
	}

	// 三维表：X的表头值
	@RequestMapping(value = "/findXTitleByfield/{field}", produces = "application/json;charset=utf-8", method = RequestMethod.GET, params = "action=get")
	@ResponseBody
	public String findXTitle(@PathVariable String field) {
		return buildCaseService.findXTitle(field);
	}

	// 三维表：Y的表头值
	@RequestMapping(value = "/findYTitleByfield/{field}", produces = "application/json;charset=utf-8", method = RequestMethod.GET, params = "action=get")
	@ResponseBody
	public String findYTitle(@PathVariable String field) {
		return buildCaseService.findYTitle(field);
	}

	// 访问给定数据库名及实例名的实例
	@RequestMapping(value = "/name/{schemaNames}/case/name/{caseNames}", produces = "application/json;charset=utf-8", method = RequestMethod.GET, params = "action=get")
	@ResponseBody
	public String visitCase(@PathVariable String schemaNames,
			@PathVariable String caseNames) {
		return buildCaseService.visitCase(schemaNames, caseNames);
	}

	// 访问某数据库下某实例下的某二维表下的某个值
	@RequestMapping(value = "/name/{schemaNames}/case/name/{caseNames}/fieldMatrixld/name/{fieldMatrixName}", produces = "application/json;charset=utf-8", method = RequestMethod.GET, params = "action=get")
	@ResponseBody
	public String visitMatrix(@PathVariable String schemaNames,
			@PathVariable String caseNames, @PathVariable String fieldMatrixName) {
		return buildCaseService.visitMatrix(schemaNames, caseNames,
				fieldMatrixName);
	}

	// visit case by caseId
	@RequestMapping(value = "/id/{caseId}", produces = "application/json;charset=utf-8", method = RequestMethod.GET, params = "action=get")
	@ResponseBody
	public String visitCase(@PathVariable String caseId) {
		return buildCaseService.visitCaseById(Integer.parseInt(caseId));
	}

	// 根据实例的id获取实例下所有的二维表
	@RequestMapping(value = "/fid/{caseId}", produces = "application/json;charset=utf-8", method = RequestMethod.GET, params = "action=get")
	@ResponseBody
	public String findByid(@PathVariable String caseId) {
		return buildCaseService.findCaseById(caseId);
	}

	// 根据实例的id查询所有图片属性项列表
	@RequestMapping(value = "/findImage/{caseId}", produces = "application/json;charset=utf-8", method = RequestMethod.GET, params = "action=get")
	@ResponseBody
	public String findImage(@PathVariable String caseId) {
		return buildCaseService.findImageBycaseId(caseId);
	}

	// 根据id访问某数据库下某实例下的某个二维表下的某行
	@RequestMapping(value = "/id/{caseId}/field/id/{fieldID}/OrdinalId/id/{OrdinalId}", produces = "application/json;charset=utf-8", method = RequestMethod.GET, params = "action=get")
	@ResponseBody
	public String visitMatrixa(@PathVariable String caseId,
			@PathVariable String fieldID, @PathVariable int OrdinalId) {
		return buildCaseService.visitMatrixById(caseId, fieldID, OrdinalId);

	}

	// 根据id访问某数据库下某实例下的某个二维表的某列
	@RequestMapping(value = "/id/{caseId}/fieldOneList/id/{fieldID}", produces = "application/json;charset=utf-8", method = RequestMethod.GET, params = "action=get")
	@ResponseBody
	public String visitOneList(@PathVariable String caseId,
			@PathVariable String fieldID) {
		return buildCaseService.visitOneList(caseId, fieldID);
	}

	// 访问某数据库下、某实例下、某属性的值
	@RequestMapping(value = "/name/{schemaNames}/case/name/{caseNames}/field/name/{fieldNames}", produces = "application/json;charset=utf-8", method = RequestMethod.GET, params = "action=get")
	@ResponseBody
	public String visitField(@PathVariable String schemaNames,
			@PathVariable String caseNames, @PathVariable String fieldNames) {
		return buildCaseService.visitField(schemaNames, caseNames, fieldNames);
	}

	// 根据实例id以及属性的id获取属性的值
	@RequestMapping(value = "/id/{caseId}/queryId/id/{fieldID}", produces = "application/json;charset=utf-8", method = RequestMethod.GET, params = "action=get")
	@ResponseBody
	public String findByCaseIDandFieldId(@PathVariable String caseId,
			@PathVariable String fieldID) {
		return buildCaseService.findResult(caseId, fieldID);
	}

	// 根据实例名称查询
	@RequestMapping(value = "/findInstance/{caseId}", produces = "application/json;charset=utf-8", method = RequestMethod.GET, params = "action=get")
	@ResponseBody
	public String findInstanceName(@PathVariable String caseId) {
		return buildCaseService.findInstanceName(caseId);
	}

	// 访问某数据库下、某实例下、某属性组的值
	@RequestMapping(value = "/name/{schemaNames}/case/name/{caseNames}/fields/name/{fieldNames}", produces = "application/json;charset=utf-8", method = RequestMethod.GET, params = "action=get")
	@ResponseBody
	public String visitFields(@PathVariable String schemaNames,
			@PathVariable String caseNames, @PathVariable String fieldNames) {
		return buildCaseService.visitFields(schemaNames, caseNames, fieldNames);
	}

	@RequestMapping(value = "/caseLink")
	@ResponseBody
	public Result getCasesToChoose(
			@RequestParam(value = "checkIds", required = true) String checkIds,
			HttpSession session) {
		Result res = new Result();
		res.setMessage(buildCaseService.getAllCasesToChoose(checkIds, session));
		return res;
	}
}
